<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">酒店管理系统</h2>
      <el-form ref="loginForm" :model="data.loginForm" :rules="data.loginRules" @submit.native.prevent="login">
        <el-form-item prop="role">
          <el-radio-group v-model="data.loginForm.role">
            <el-radio label="user">用户</el-radio>
            <el-radio label="admin">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item prop="username">
          <el-input
            v-model="data.loginForm.phoneOrEmail"
            :placeholder="data.loginForm.role === 'admin' ? '请输入管理员账号' : '请输入邮箱'"
            prefix-icon="el-icon-user"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="data.loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            style="width: 100%"
            @click="login"
          >
            登录
          </el-button>
        </el-form-item>
        <div class="register-link" v-if ="data.loginForm.role === 'user'">
        <span>还没有账户？</span>
        <span class="register-text" @click="goToRegister"  style="color:#559afe">立即注册</span>
      </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import  axios  from 'axios'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
  
  const router = useRouter()
  const formRef = ref()
  const data = reactive({
      loginForm: {
        phoneOrEmail: '',
        password: '',
        role: 'user'
      },
      loginRules: {
        email: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    })
    const login = () => {
  axios.post("/api/chapter04/login", data.loginForm)
    .then(res => {
      if (res.data.status === 'success' && data.loginForm.role === 'admin' ) {
        const token = res.data
        if (!token) {
          throw new Error('No token in response')
        }
        localStorage.setItem('token', token)
        ElMessage.success('登录成功')
        console.log(data.loginForm)
        console.log('Token stored in localStorage:', localStorage.getItem('token'))
        router.push('/admin/dashboard')
      } else if(res.data.status === 'success' && data.loginForm.role === 'user'){
        const token = res.data
        if (!token) {
          throw new Error('No token in response')
        }
        localStorage.setItem('token', token)
        ElMessage.success('登录成功')
        console.log(data.loginForm)
        console.log('Token stored in localStorage:', localStorage.getItem('token'))
        router.push('/')
      }else{
        ElMessage.error(res.data.message)
      }
    })
    .catch(error => {
      ElMessage.error('登录失败')
      console.error(error)
    })
}
const goToRegister = () => {
  router.push('/register') // 确保你有 /register 页面
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 400px;
  padding: 20px;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
}
</style>